package net.hctek.AnnouncerStation.service.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.hctek.AnnouncerStation.bean.Counts;
import net.hctek.AnnouncerStation.bean.Files;
import net.hctek.AnnouncerStation.bean.Navs;
import net.hctek.AnnouncerStation.bean.User;
import net.hctek.AnnouncerStation.dao.impl.CountsDao;
import net.hctek.AnnouncerStation.dao.impl.FilesDao;
import net.hctek.AnnouncerStation.dao.impl.NavsDao;
import net.hctek.AnnouncerStation.dao.impl.UserDao;
import net.hctek.AnnouncerStation.service.IdentityServiceI;
import net.hctek.AnnouncerStation.util.MD5;
import net.sf.json.JSONObject;


@Service
@Transactional(readOnly=false,rollbackFor=java.lang.Exception.class)
public class IdentityService implements IdentityServiceI{

	@Autowired
	private UserDao userDao;
	@Autowired
	private FilesDao filesDao;
	@Autowired
	private NavsDao navsDao;
	@Autowired
	private CountsDao countsDao;
	
	
	
	
	@Transactional(readOnly=true)
	@Override
	public User getUserById(String userId) {
		try {
			User u = new User();
			u = null;
			List<User> us = userDao.getUserByName(userId);
			System.out.println(us.toString());
			if(!us.equals("[]")){
				for(int i=0;i<us.size();i++)
				u = us.get(i);
			}
			return u;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//创建用户
	@Override
	public void savaUser(User user) {
		try {
			userDao.save(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//
	@Override
	public List<String> getUserNameAll() {
		try {
			return userDao.getUserByIdAll();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("");
		}
		return null;
	}
	//查询所以用户
	@Override
	public List<User> selectUser() {
		try {
			return userDao.getUserAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	//
	@Override
	public String updateUser(User user) {
		try {
			User u = userDao.get(User.class, user.getUserId());
			if(user.getPassWord().equals(u.getPassWord())){
				u.setPassWord(user.getPassWord());
			}else{
				String passwordx = MD5.EncoderByMd5(user.getPassWord());
				u.setPassWord(passwordx);
			}
			SimpleDateFormat bartDateFormat = new SimpleDateFormat
	  				("yyyy-MM-dd"); 
			Date date = new Date(); 
			System.out.println(bartDateFormat.format(date)); 
			u.setDelFlag("1");
			//u.setUserLanguage(user.getUserLanguage());
			u.setUserMailBox(user.getUserMailBox());
			u.setUserPhoneNumBer(user.getUserPhoneNumBer());
			//u.setUserRace(user.getUserRace());
			u.setUserSex(user.getUserSex());
			u.setUserTime(bartDateFormat.format(date));
			return "";
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	@Override
	public void deleteUserByIds(String userId) {
		try {
			User u = getUserById(userId);
			System.out.println("delete:"+u.toString());
			u.setDelFlag("0");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	@Override
	public boolean ajaxValidName(String userId) {
		try {
			List<User> us = userDao.ValidName(userId);
			System.out.println(us.toString());
			System.out.println(us.toString().equals("[]"));
			if(!us.toString().equals("[]")){
				System.out.println("true");
				return true;
			}else{
				System.out.println("false");
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	//判断文件名是否重复
	@Override
	public List<Files> Duplicatefiles(String filename,String files) {
		try {
			return filesDao.repeatfile(filename,files);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void savaFiles(List<Files> filess) {
		
		try {
			for(int i=0;i<filess.size();i++){
				Files files = filess.get(i);
				filesDao.save(files);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//异步加载文件信息
	@Override
	public String ajaxLoadFile() {
		try {
			
			List<Map<String,String>> ajaxFiles = filesDao.ajaxLoadFile();
			JSONObject obj = new JSONObject();
			obj.put("filesjson", ajaxFiles);
			System.out.println(obj.toString());
			return obj.toString();
			/*return ajaxFiles.toString();*/
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}



	@Override
	public String showFiles() {
		try {
			List<Map<String,String>> ajaxFile = filesDao.ajaxFile();
			JSONObject obj = new JSONObject();
			obj.put("filesjson", ajaxFile);
			System.out.println(obj.toString());
			return obj.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Navs> getNavsAll() {
		try {
			 return navsDao.getAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Files> DuplicateFiles(String string, String string2) {
		try {
			return filesDao.repeatfile(string, string2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Map<String,String>> showFilesList() {
		try {
			return filesDao.showFilesList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Long UserCount() {
		try {
			List<Long> temp = userDao.UserCount();
			System.out.println(temp.iterator());
			Long userCount = temp.get(0).longValue();
			return userCount;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Long NewUserCount(String Data) {
		try {
			List<Long> temp = userDao.newUserCount(Data);
			Long newUserCount = temp.get(0).longValue();
			System.out.println(newUserCount);
			return newUserCount;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Long NewSongCount(String Data) {
		try {
			List<Long> temp = filesDao.newSongCount(Data);
			Long newUserCount = temp.get(0).longValue();
			System.out.println(newUserCount);
			return newUserCount;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Long SongCountAll() {
		try {
			List<Long> temp = filesDao.SongCountAll();
			Long SongCountAll = temp.get(0).longValue();
			System.out.println(SongCountAll);
			return SongCountAll;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Counts getCount(String name) {
		try {
			List<Counts> temp = countsDao.getAll(name);
			Counts QrcodeCountAll = temp.get(0);
			return QrcodeCountAll;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void updataQrcodeCount(Long i,String data) {
		try {
			Counts QrcodeCount = getCount("生成二维码次数");
			QrcodeCount.setCountsSum(QrcodeCount.getCountsSum()+i);
			QrcodeCount.setCountsTime(data);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public Long QrcodeCountAll() {
		try {
			List<Long> temp = countsDao.getQrcodeCount("生成二维码次数");
			return temp.get(0).longValue();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void updataDownloadCount(Long i, String data) {
		try {
			Counts counts = getCount("下载次数");
			counts.setCountsSum(counts.getCountsSum()+1);
			counts.setCountsTime(data);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public Long DownloadCountAll() {
		try {
			List<Long> temp = countsDao.getQrcodeCount("下载次数");
			return temp.get(0).longValue();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	

}